package com.hnevc.xiaoming.httpactivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.httpbean.MyLogInBean;
import com.hnevc.xiaoming.httpbean.RePassWordBean;

import org.json.JSONException;
import org.json.JSONObject;

public class MyVolleyLogIn extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private TextView title_view;
    private EditText et_name;
    private EditText et_password;
    private Button btn_denglu;
    private Button btn_zhuce;
    private String name;
    private String password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_log_in);
        initView();


    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        title_view = (TextView) findViewById(R.id.title_view);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_view = (TextView) findViewById(R.id.title_view);
        title_view.setText("登錄界面");

        et_name = (EditText) findViewById(R.id.et_name);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_denglu = (Button) findViewById(R.id.btn_denglu);
        btn_zhuce = (Button) findViewById(R.id.btn_zhuce);

        btn_denglu.setOnClickListener(this);
        btn_zhuce.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_denglu:

                if (submit()==true) {
                    //网络地址
                    String url = "http://anli-wechat.boruiman.com/api/UserServer/user_login";

                    //body傳遞給服務器的參數
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("username", name);
                        jsonObject.put("password", password);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    //创建队列
                    RequestQueue requestQueue = Volley.newRequestQueue(MyVolleyLogIn.this);
                    //请求主体，1请求方式，2请求地址，3请求参数，4 5 分别是成功与失败响应监听
                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
                            jsonObject, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            Gson gson = new Gson();
                            //将响应成功的json数据映射到实体类当中；
                            MyLogInBean myLogInBean = gson.fromJson(jsonObject.toString(), MyLogInBean.class);
                            Toast.makeText(MyVolleyLogIn.this, "" + myLogInBean.getItem().getUsername(), Toast.LENGTH_SHORT).show();
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Toast.makeText(MyVolleyLogIn.this, "网络请求失败！", Toast.LENGTH_SHORT).show();
                        }
                    });

                    //加入网络队列
                    requestQueue.add(jsonObjectRequest);
                }

                break;
            case R.id.btn_zhuce:
                startActivity(new Intent(MyVolleyLogIn.this,MyVolleyRegistered.class));
                break;
        }
    }

    private boolean submit() {
        // validate
        name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "password不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
       return true;
    }
}
